import { Button, Popconfirm, Icon, Radio } from 'antd';
import { inject, observer } from 'mobx-react';
import { WeaTop, WeaRightMenu, WeaTools, WeaDialog, WeaRadioGroup, WeaInputSearch, WeaErrorPage,WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
import { WeaTableNew } from 'comsMobx';
import {WeaAddNotice} from 'weaPortalPublic';
const WeaTable = WeaTableNew.WeaTable;
import './css/style.css';
@inject('newnotice_more_store')
@observer
class NewNoticeMore extends React.Component {
    constructor(props) {
        super(props)
        this.getButtons = this.getButtons.bind(this);
        this.getRightMenu = this.getRightMenu.bind(this);
        this.onRightMenuClick = this.onRightMenuClick.bind(this);
        this.onOperatesClick = this.onOperatesClick.bind(this);
        this.doAdd = this.doAdd.bind(this);
       	const { eid } = props.location.query;
        const { canAddNotice } = props.location.query;
        //初始化数据
        this.props.newnotice_more_store.initParam(eid,canAddNotice);
        this.getColumns = this.getColumns.bind(this);
    }
    getButtons(){
        const { logintemplatename, getSessionKey, saveLoginTemplate, deleteRows, tableStore, doAdd, canAddNotice } = this.props.newnotice_more_store;
        const { eid } = this.props.location.query;
        const {selectedRowKeys} = tableStore;
        const __keys = selectedRowKeys.slice();
        let btns = [];
        btns.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@xs4fw6`} type="primary" disabled={!canAddNotice} onClick={()=>this.doAdd({isNew: true, visible: true, isView: false})}>{getLabel(387993,"新建公告")}</Button>);
        btns.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@g2i7mp`} type="primary" disabled={!(__keys && __keys.length)} onClick={()=>deleteRows(eid)}>{getLabel(32136, '批量删除')}</Button>);
        btns.push(<WeaInputSearch ecId={`${this && this.props && this.props.ecId || ''}_WeaInputSearch@oeuze7`} placeholder={getLabel(84078,"请输入名称")} value={logintemplatename} onSearch={value => getSessionKey({current: 1,logintemplatename: value,eid: eid,canAddNotice:canAddNotice})}/>);
        return btns;
    }
    getRightMenu(){
        const { tableStore, canAddNotice } = this.props.newnotice_more_store;
        const {selectedRowKeys} = tableStore;
        const __keys = selectedRowKeys.slice();
        let btns = [];
        btns.push({
            icon: <i className='icon-coms-Preservation'/>,
            disabled: !canAddNotice,
            content:getLabel(387993,"新建公告")
        });
        btns.push({
            disabled: !(__keys && __keys.length),
            icon: <i className='icon-coms-delete'/>,
            content:getLabel(32136, '批量删除')
        });
        return btns
    }
    onRightMenuClick(key){
       const { saveLoginTemplate, deleteRows, doAdd, canAddNotice } = this.props.newnotice_more_store;
       const { eid } = this.props.location.query;
       switch(key){
            case '0':
                this.doAdd({isNew: true, visible: true, isView: false});
                break;
            case '1':
                deleteRows(eid)
                break;
       }
    }
    onOperatesClick(record,index,operate,flag){
        let title = record.title;
        let content = record.content;
        let imgsrc = record.imgsrc;
        const id = record.randomFieldId;
        const { newnotice_more_store } = this.props;
        const { eid } = this.props.location.query;
        switch(operate.index){
            case '0':
                this.doAdd({ isNew: false, imgsrc, title, content, id, visible: true, isView: false});
                break;
            case '1':
                newnotice_more_store.doDel(id,eid);
                break;
        }
    }
    doAdd(state){
      	this.refs.addNotice.onSetState(state);
    }
    getColumns(oldColumns) {
        if(oldColumns && oldColumns.length){
            oldColumns[0].render = (text, record, index) => {
            	let title = record.title;
		        let content = record.content;
		        let imgsrc = record.imgsrc;
		        const id = record.randomFieldId;
                return <div className="wea-url"><a href="javascript:void(0);" onClick={() => this.doAdd({ isNew: false, imgsrc, title, content, id, visible: true, isView: true})}>{text}</a></div>
            }
        }
        return oldColumns;
    }
    render(){
        const { status, sessionkey, tableStore, handleRefresh, canAddNotice} = this.props.newnotice_more_store;
        const { eid } = this.props.location.query;
        if(!sessionkey) return <div/>
        return <div className="portal-loginportal-box" id="loginPortal">
            <WeaAddNotice ecId={`${this && this.props && this.props.ecId || ''}_WeaAddNotice@554r4k`} ref="addNotice" eid={eid} handleRefresh={()=>handleRefresh(eid)} canAddNotice={canAddNotice}/>
            <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@ac9hj5`} title={getLabel(23666,"公告")}
                    loading={false}
                    icon={<i className='icon-coms-portal' />}
                    iconBgcolor='#1a57a0'
                    buttons={this.getButtons()}
                    buttonSpace={10}
                    showDropIcon={true}
                    dropMenuDatas={this.getRightMenu()}
                    onDropMenuClick={this.onRightMenuClick}>
                   <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@qd6y5j`} datas={this.getRightMenu()} onClick={this.onRightMenuClick}>
                       <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@3rybt2`} comsWeaTableStore={tableStore} getColumns={this.getColumns} onOperatesClick={this.onOperatesClick}/>
                   </WeaRightMenu>
            </WeaTop>
        </div>
    }

}

class MyErrorHandler extends React.Component {
    render() {
        const hasErrorMsg = this.props.error && this.props.error !== "";
            return ( <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@xly6fj`} msg = { hasErrorMsg ? this.props.error : getLabel(385218,"对不起，该页面异常，请联系管理员！") }/>
        );
    }
}
NewNoticeMore = WeaTools.tryCatch(React, MyErrorHandler, { error: "" })(NewNoticeMore);

export default NewNoticeMore;